#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>
using namespace std;
class Solution
{
public:
    int missingNumber(vector<int> &nums)
    {
        int maxV = nums.size();
        for (int i = maxV - 1; i >= 0; i--)
        {
            if (find(nums.begin(), nums.end(), i) == nums.end())
            {
                return i;
            }
        }
        return maxV;
    }
    int missingNumber_3(vector<int> &nums)
    {
        unordered_set<int> set;
        int n = nums.size();
        for (int i = 0; i < n; i++)
        {
            set.insert(nums[i]);
        }
        int missing = -1;
        for (int i = 0; i <= n; i++)
        {
            if (!set.count(i))
            {
                missing = i;
                break;
            }
        }
        return missing;
    }
    /**
     * @brief best
     * 
     * @param nums 
     * @return int 
     */
    int missingNumber_2(vector<int> &nums)
    {
        int ans = 0;
        int n = nums.size();
        for (int i = 0; i < n; i++)
        {
            ans ^= nums[i];
        }
        for (int i = 0; i <= n; i++)
        {
            ans ^= i;
        }
        return ans;
    }

    int missingNumber_4(vector<int> &nums)
    {
        int n = nums.size();
        int total = n * (n + 1) / 2;
        int arrSum = 0;
        for (int i = 0; i < n; i++)
        {
            arrSum += nums[i];
        }
        return total - arrSum;
    }
};
int main()
{
    Solution s;
    vector<int> nums = {9, 6, 4, 2, 3, 5, 7, 0, 1};
    cout << s.missingNumber(nums) << endl;
    system("pause");
    return 0;
}
